<?php
class indexAction extends baseAction{
	
	function index(){
		if(Cookie('userName')){
			$userName = Cookie('userName');
			$user = D("ju_users")->where("user_name='".$userName."'")->find();
			$this->assign("user",$user);
		}
		
		$item_mod = D("ju_item");
		$item = $item_mod->order("sort desc")->limit("0,20")->select();
		//list($id,$item['item_id']);
		$item_ids = "";
		$outputItem = array();
		foreach($item as $k =>$v){
			$item_ids .= $v['item_id'] .",";
			$outputItem[$v['item_id']] = $v;
		}
		
		$item_ids = substr($item_ids,0,strlen($item_ids)-1);
		//echo $item_ids;
		
		$taoke_items = $this->converItem($item_ids);
		
		foreach($taoke_items['taobaoke_items']['taobaoke_item'] as $k=>$v){
			//print_r($v['num_iid']);
			$outputItem["".$v['num_iid']] = array_merge($v,$outputItem["".$v['num_iid']]) ;
		}
		$out = array();
		foreach($outputItem as $k=>$v){
			$out[] = $v;
		}
		
		$all = count($out);
		
		for($i=0;$i<($all/3);$i++){
			$items0[] = $out[$i];
		}
		for($j=$i++;$j<($all/3)+$i-1;$j++){
			$items1[] = $out[$j];
		}
		for($i=$j++;$i<($all/3)+$j-1;$i++){
			$items2[] = $out[$i];
		}
		//print_r($items1);
		$this->assign("items0",$items0);
		$this->assign("items1",$items1);
		$this->assign("items2",$items2);
		$this->display();
	}
	
	public function product(){
		
		$rs = D("ju_item");
		$item = $rs->where("id=".$_GET['id'])->find();
		
		$this->assign("product",$item);
		//print_r($item[0]);
		//click+1
		$rs->where("id=".$_GET['id'])->setField(array("click"=>"click+1"));
		
		$waterFall = $rs->where(" id>8 ")->order(" rand() ")->limit("0,30")->select();
		$this->assign('waterFall',$waterFall);
		
		//ad
		$rs = D("ju_slide");
		$ad = $rs->where("id=11")->select();
		$this->assign("ad",$ad[0]);
		//print_r($ad['picUrl']);
		//end of ad

		
		$this->display();
	}
	
	public function categray(){
		$rs = D("ju_item");
		//$pager = new pager();
		//iewPage($totalcount,$totalpage,$pagenum,$pagesize,$param,$url)
		//pagenum 但前页面；
		$pageSize = 24;
		$page = $_GET['page'];
		if($page<1)
			$page = 1;
		
		$limit = ($page-1) * $pageSize .",". $pageSize;
		
		//echo $sql;
		$cla = D("ju_class");
		$claType = $cla->where("id=".$_GET['classId'])->select();
		if($claType[0]['p_id']==0){
			$subType = $cla->where("p_id=".$claType[0]['id'])->select();//取大类的所有小类；
			$subClassId = array();
			foreach($subType as $k=>$v){
				$subClassId[] =  $v['id'];
			}
			
			$classId = implode(",",$subClassId);
			if($classId==''){
				$classId = $_GET['classId'];
			}
		}else{
			$classId = $_GET['classId'];
		}
		
		$totalNums = $rs->where("classId in(".$classId.")")->count("id");
		
		$items = $rs->where("classId in(".$classId.")")->limit($limit)->select();
		//echo $rs->getLastSql();
		
		
		$returnItems = array();
		for($i=0;$i<12;$i++){
			$returnItems[] = $items[$i]; 
		}
		$this->assign("items",$items);

		//print_r($totalNums);
		
		//echo $totalNums/24;
//		$p = $pager->ViewPage($totalNums,$totalNums/24,$page,24,"a=categray&classId=".$_GET['classId'],"/index.php");
		
		$cla = D("ju_class");
		$classType = $cla->where("id=".$_GET['classId'])->select();
		if($classType[0]['p_id']==0){
			$subClass = $cla->where("p_id=".$_GET['classId'])->select();
		}else{
		
			$subClass = $cla->where("p_id=".$classType[0]['p_id'])->select();
		}
		
		
		$this->assign("subClass",$subClass);

		$this->assign("currentPage",$page);
		
		$this->assign("page",$p);
		
		$this->display();
	}
	public function test(){
		

		$this->display();
	}
	
	
	function converItem($item_id){
		$app_key = C('APP_KEY');
		$secret=C('APP_SECRET');
		$timestamp=time()."000";
		$message = $secret.'app_key'.$app_key.'timestamp'.$timestamp.$secret;
		$mysign=strtoupper(hash_hmac("md5",$message,$secret));
		$num_iids = $item_id;
		$ch = curl_init();
		$url = "http://gw.api.taobao.com/widget/rest?callback=&_t_sys=args%3D4&app_key=".$app_key."&is_mobile=true&fields=title%2Cnum_iid%2Cclick_url%2Ciid%2Ccommission%2Ccommission_rate%2Ccommission_num%2Ccommission_volume%2Ccommission_rate&method=taobao.taobaoke.widget.items.convert&num_iids=".$num_iids."&partner_id=top-sdk-js-20120801&sign=".$mysign."&timestamp=".$timestamp;
		
		curl_setopt($ch, CURLOPT_REFERER, "http://tuan.wantujie.com");
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		$resp = curl_exec($ch);
		$resp=json_decode($resp);
		$resp = object_array($resp);
		
		return $resp;

	}
}
?>